'''
Created on 2009-12-13

@author: Jialai_Zhu
'''
import re
from binascii import *
from unescape import *
def xor1b(c,seed):
    try:
        h1   = int(c,16)
        h2   = int(seed,16)
        hxor = h1^h2
        axor = "%x"%(hxor)
#        print h1,axor
        return axor
    except:
        return None


def xor(data,seed='bd'):
    def replace(matchobj):
        c=matchobj.group(0)   
#        print c
        return xor1b(c,seed)            
    p=re.compile(r"[a-f0-9]{2}",re.IGNORECASE)
    ret=p.sub(replace,data)
    return ret
def find_xor_keys(data):
    p = re.compile(r"%u[a-f0-9]{2}%",re.IGNORECASE)
    ret=[]
    for i in p.findall(data):
        ret.append(i[2:4])
    if len(ret):       
        return ret
    
    p = re.compile(r"%ubdbd",re.IGNORECASE)
    for i in p.findall(data):
        ret.append(i[2:4])
    if len(ret):       
        return ret

def check_xor(page):
#    s=('bd':"\\x92\\x92")
    keys=find_xor_keys(page)
    data=xstrcat(page)
    key=keys[0]    
    sig=xor("\\x2f\\x2f",key)
    pos=data.find(sig)
    if pos!=-1:
        ret=xor(data,key)
        ret=unescapeX(ret)
        return ret
    return None
print xor(r'\x92\x92\xCD\xCD\xDA\x93\xC8\xC8\xDF\x93\xDE\xDE\x92\xD1\x92\xC5\xC5\x93\xD8\xC5\xD8\xBD') 